package com.ruoyi.comment.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dynamic.domain.PwDynamic;
import com.ruoyi.dynamic.mapper.PwDynamicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comment.mapper.PwCommentMapper;
import com.ruoyi.comment.domain.PwComment;
import com.ruoyi.comment.service.IPwCommentService;

/**
 * 评论表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-29
 */
@Service
public class PwCommentServiceImpl implements IPwCommentService 
{
    @Autowired
    private PwCommentMapper pwCommentMapper;
    @Autowired
    private PwDynamicMapper pwDynamicMapper;

    /**
     * 查询评论表
     * 
     * @param id 评论表主键
     * @return 评论表
     */
    @Override
    public PwComment selectPwCommentById(Long id)
    {
        return pwCommentMapper.selectPwCommentById(id);
    }

    /**
     * 查询评论表列表
     * 
     * @param pwComment 评论表
     * @return 评论表
     */
    @Override
    public List<PwComment> selectPwCommentList(PwComment pwComment)
    {
        return pwCommentMapper.selectPwCommentList(pwComment);
    }

    /**
     * 新增评论表
     * 
     * @param pwComment 评论表
     * @return 结果
     */
    @Override
    public int insertPwComment(PwComment pwComment)
    {
        pwComment.setCreateTime(DateUtils.getNowDate());
        //判断是否是动态
        if(pwComment.getType().equals(PwComment.COMMENT_TYPE_DYNAMIC)){
            //获取动态
            PwDynamic dynamic = pwDynamicMapper.selectPwDynamicById(pwComment.getCommentId());
            dynamic.setCommentNumber(dynamic.getCommentNumber()+1);
            pwDynamicMapper.updatePwDynamic(dynamic);
        }
        int i = pwCommentMapper.insertPwComment(pwComment);
        return i;
    }

    /**
     * 修改评论表
     * 
     * @param pwComment 评论表
     * @return 结果
     */
    @Override
    public int updatePwComment(PwComment pwComment)
    {
        pwComment.setUpdateTime(DateUtils.getNowDate());
        return pwCommentMapper.updatePwComment(pwComment);
    }

    /**
     * 批量删除评论表
     * 
     * @param ids 需要删除的评论表主键
     * @return 结果
     */
    @Override
    public int deletePwCommentByIds(Long[] ids)
    {
        for (Long id : ids) {
            PwComment comment = pwCommentMapper.selectPwCommentById(id);
            if(comment.getType().equals(PwComment.COMMENT_TYPE_DYNAMIC)){
                //获取动态
                PwDynamic dynamic = pwDynamicMapper.selectPwDynamicById(comment.getCommentId());
                dynamic.setCommentNumber(dynamic.getCommentNumber()-1);
                pwDynamicMapper.updatePwDynamic(dynamic);
            }
        }
        return pwCommentMapper.deletePwCommentByIds(ids);
    }

    /**
     * 删除评论表信息
     * 
     * @param id 评论表主键
     * @return 结果
     */
    @Override
    public int deletePwCommentById(Long id)
    {
        //先获取评论
        PwComment comment = pwCommentMapper.selectPwCommentById(id);
        if(comment.getType().equals(PwComment.COMMENT_TYPE_DYNAMIC)){
            //获取动态
            PwDynamic dynamic = pwDynamicMapper.selectPwDynamicById(comment.getCommentId());
            dynamic.setCommentNumber(dynamic.getCommentNumber()-1);
            pwDynamicMapper.updatePwDynamic(dynamic);
        }

        return pwCommentMapper.deletePwCommentById(id);
    }


}
